﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace linkouqs.Gamekit {

    public sealed class Stone : TakeableObject {

        //public Transform follow;

        //void Update() {
        //    if(follow == null) {
        //        return;
        //    }
        //    transform.position = Vector2.Lerp(transform.position, follow.position, 0.9f);
        //}

        protected override void OnTriggerEnter2D(Collider2D collision) {
            if (!isThrowing) {
                return;
            }
            OnBroken();
        }

        public override void OnBroken() {
            Debug.Log("Broken");
            brokenFX.transform.SetParent(null);
            brokenFX.gameObject.SetActive(true);
            gameObject.SetActive(false);
        }

        public override void OnThrow() {
            gameObject.layer = LayerMask.NameToLayer("Stone");
            isThrowing = true;
            var r2d = GetComponent<Rigidbody2D>();
            if (r2d != null) {
                r2d.velocity = Vector2.zero;
                r2d.simulated = true;
                r2d.gravityScale = 0f;
            }
        }
        
        public override void OnTake() {
            trigger.SetActive(false);
            var r2d = GetComponent<Rigidbody2D>();
            if (r2d != null) {
                r2d.simulated = false;
                r2d.gravityScale = 0f;
            }
            gameObject.layer = LayerMask.NameToLayer("CollisionFreeObject");
            var c2d = GetComponent<Collider2D>();
            if (c2d != null) {
                c2d.isTrigger = true;
            }
        }

    }
}